package com.peas.platform.module.icims.expenditure.disbursement.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.disbursement.adapter.repository.ExpenditureTreasuryRepository;
import com.peas.platform.module.icims.expenditure.disbursement.adapter.repository.po.ExpenditureTreasuryPo;
import com.peas.platform.module.icims.expenditure.disbursement.domain.ExpenditureTreasuryDomain;
import com.peas.platform.module.icims.expenditure.disbursement.dto.ExpenditureTreasuryDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ExpenditureTreasuryDomainImpl extends BaseDomainImpl<ExpenditureTreasuryDto, ExpenditureTreasuryPo> implements ExpenditureTreasuryDomain {

    @Resource
    private ExpenditureTreasuryRepository expenditureTreasuryRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ExpenditureTreasuryPo> getMapper() {
        return expenditureTreasuryRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureTreasuryPo>> getRepositoryClass() {
        return ExpenditureTreasuryRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureTreasuryDto dto) {
        if (StrUtil.isBlank(dto.getTreasuryCode())) {
            String code = sequenceService.genSequence("expenditure", "ExpenditureTreasuryDto", "ETD", 4, 1);
            dto.setTreasuryCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("treasuryCode", dto.getTreasuryCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureTreasuryDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("treasuryCode", dto.getTreasuryCode());
        return result;
    }

}
